import createHistory from 'history/createBrowserHistory'
import { Modal, Toast } from 'antd-mobile'
import Clipboard from 'clipboard'
import queryString from 'query-string'
import Helper from './helper'
import {pipeline} from '../helper/fetching'
const history = createHistory({
  forceRefresh: true, // 从服务器重新拉取数据
})

class Position extends Helper{
  constructor(name) {
    super()
    this.name = name
    this.init()
  }

  init() {
    this.ownShare()
    this.collectPosition()
    this.toEmploy()
    this.zhugeJubao()
  }

  collectPosition() { // 收藏职位
    const $ = this.$, me = $('.js_cllect')
    me.on('click', () => {
      const {job_id, is_favorited} = me.data(), img = me.find('img'), span = me.find('span');
      const _src = img.attr('_src'), src = img.attr('src')
      if(is_favorited) { // 取消收藏
        pipeline(':ve.mobile.interface/user/delete_favorite_job' ,{job_id})
          .then(() => {
            Toast.success('取消收藏', 2)
            img.attr('_src', src)
            img.attr('src', _src)
            me.data('is_favorited', 0)
            span.html('收藏')
            window.zhuge.track('取消收藏职位')
          })
      } else { // 收藏
        pipeline(':ve.mobile.interface/user/add_favorite_job' ,{job_id})
          .then(data => {
            if (data.status === 0){
              const msg = data.errMsg
              const pathname = window.location.pathname
              if(msg === '未登陆') {
                return this.goLogin(history, `${pathname}`)
              }
              return this.Toast(msg, 2)
            } else { // success
              Toast.success('收藏成功', 2)
              img.attr('_src', src)
              img.attr('src', _src)
              me.data('is_favorited', 1)
              span.html('取消')
              window.zhuge.track('收藏职位')
            }
          })
      }
    })
  }

  toEmploy() { // 应聘职位
    const $ = this.$, me = $('.js_toEmploy')
    me.on('click', () => {
      const {isApplied, job_id} = me.data()
      const {from} = queryString.parse(window.location.search)
      if(!isApplied) {
        pipeline(':ve.mobile.interface/user/apply',{
          job_id,
          from,
          client_id: 4,
        }).then(data => {
          if (data.status === 0){
            const msg = data.errMsg
            const pathname = window.location.pathname
            if(msg === '未登陆') {
              return this.goLogin(history, `${pathname}`)
            }
            return this.Toast(msg, 2)
          }
          me.html('已投递').addClass('isApplied').data('isApplied', 1)
          Toast.success('投递成功', 2)
          window.zhuge.track('应聘职位')
        })
      }
    })
  }

  ownShare() {
    const shareLink = window.location.href,$ = this.$,me = $('.js_share')
    me.on('click', () => {
      new Clipboard(me.selector, {
        text: () => `${shareLink}`,
      })
      window.zhuge.track('分享')
      if (navigator.userAgent.indexOf('UCBrowser') > -1 && window.ucbrowser) { // uc  浏览器
        const shareArgs = ['简历分享', '快来看看我的简历吧', shareLink, '', '', '\n@' + window.location.host, '']
        return window.ucbrowser.web_share(...shareArgs)
      }
      Modal.alert(
        '链接已经复制到剪贴板', shareLink, [
          { text: '确定', style: 'default' },
        ])
    })
  }

  zhugeJubao() {
    const $ = this.$, jubao = $('.zhuge_jubao'), goCompany = $('.zhuge_goCompany'), zhuge_more = $('.zhuge_more')
    jubao.on('click', () => {
      window.zhuge.track('举报该职位')
    })

    goCompany.on('click', () => {
      window.zhuge.track('点击公司名')
    })

    zhuge_more.on('click', () => {
      window.zhuge.track('点击展开职位介绍')
    })

  }

}

export default Position